#include "soundmanager.h"

//#include "introOgg.h"
//#include "still_alive.h"

//#include "ptl_missed.h"

extern u8 introOgg_ogg[];
extern u8 still_alive_ogg[];
extern u8 still_dead_ogg[];
extern u8 portii_ogg[];
extern u8 ptl_missed_raw[];
extern u8 create_fx_raw[];
extern u8 delete_fx_raw[];
extern u8 menu_click_raw[];
extern u8 portal_enter_raw[];
extern u8 portal_gun_raw[];

extern u8 typewriter_raw[];
extern u8 typewriter2_raw[];
extern u8 typewriter3_raw[];
extern u8 typewriter4_raw[];

extern u8 bloop_raw[];
extern u8 dead_raw[];
extern u8 cake_raw[];

extern u32 introOgg_ogg_size;
extern u32 still_alive_ogg_size;
extern u32 still_dead_ogg_size;
extern u32 ptl_missed_raw_size;
extern u32 create_fx_raw_size;
extern u32 delete_fx_raw_size;
extern u32 menu_click_raw_size;
extern u32 portal_enter_raw_size;
extern u32 portal_gun_raw_size;

extern u32 typewriter_raw_size;
extern u32 typewriter2_raw_size;
extern u32 typewriter3_raw_size;
extern u32 typewriter4_raw_size;

extern u32 bloop_raw_size;
extern u32 dead_raw_size;
extern u32 cake_raw_size;
extern u32 portii_ogg_size;

SoundManager::SoundManager(Settings* settingsPtr)
{
   _ptrSettings = settingsPtr;

   SND_Init(INIT_RATE_48000); // Initialize the Sound Lib

   SND_Pause(0); // the sound loop is running now

   for(int i = 0; i < Sound::SIZE; i++)
   {
       _sounds.push_back(new SoundFile((Sound::Type)i));
   }

   _sounds[Sound::INTRO]->Init(introOgg_ogg, introOgg_ogg_size, Format::OGG);
   _sounds[Sound::STILL_ALIVE]->Init(still_alive_ogg, still_alive_ogg_size, Format::OGG);
   _sounds[Sound::STILL_DEAD]->Init(still_dead_ogg, still_dead_ogg_size, Format::OGG);
   _sounds[Sound::PORTII]->Init(portii_ogg, portii_ogg_size, Format::OGG);
   
   
   _sounds[Sound::ENTER_TEST]->Init(ptl_missed_raw, ptl_missed_raw_size, Format::RAW);
   _sounds[Sound::CREATE_FX]->Init(create_fx_raw, create_fx_raw_size, Format::RAW);
   _sounds[Sound::DELETE_FX]->Init(delete_fx_raw,delete_fx_raw_size, Format::RAW);
   _sounds[Sound::MENU_CLICK_FX]->Init(menu_click_raw,menu_click_raw_size, Format::RAW);
   _sounds[Sound::PORTAL_ENTER_FX]->Init(portal_enter_raw, portal_enter_raw_size, Format::RAW);
   _sounds[Sound::PORTAL_GUN_FX]->Init(portal_gun_raw, portal_gun_raw_size, Format::RAW);
   
   _sounds[Sound::TYPEWRITER1_FX]->Init(typewriter_raw, typewriter_raw_size, Format::RAW);
   _sounds[Sound::TYPEWRITER2_FX]->Init(typewriter2_raw, typewriter2_raw_size, Format::RAW);
   _sounds[Sound::TYPEWRITER3_FX]->Init(typewriter3_raw, typewriter3_raw_size, Format::RAW);
   _sounds[Sound::TYPEWRITER4_FX]->Init(typewriter4_raw, typewriter4_raw_size, Format::RAW);
   
   _sounds[Sound::BLOOP_FX]->Init(bloop_raw, bloop_raw_size, Format::RAW);
   _sounds[Sound::DEAD_FX]->Init(dead_raw, dead_raw_size, Format::RAW);
   _sounds[Sound::CAKE_FX]->Init(cake_raw, cake_raw_size, Format::RAW);
}

SoundManager::~SoundManager()
{

}

int SoundManager::PlaySound(Sound::Type snd, int loops)
{
	if(_sounds[snd] != NULL)
	{
			switch(_sounds[snd]->GetFormat())
			{
					case Format::OGG:
							if(_ptrSettings->IsMusicEnabled())
							{
								if(loops == 0)
								{
									  StopOgg();
									  PlayOgg(mem_open((char*)_sounds[snd]->GetSoundData(), _sounds[snd]->GetSoundSize()), 0, OGG_ONE_TIME);
									  return 1;
								}
								else
								{
									  StopOgg();
									  PlayOgg(mem_open((char*)_sounds[snd]->GetSoundData(), _sounds[snd]->GetSoundSize()), 0, OGG_INFINITE_TIME);
									  return 1;
								}
							}
							
							return 1;
							break;
					case Format::RAW:
							if(_ptrSettings->IsSoundEnabled())
							{
								SND_SetVoice(SND_GetFirstUnusedVoice(), VOICE_MONO_8BIT, 8000, 0, (char*)_sounds[snd]->GetSoundData(), _sounds[snd]->GetSoundSize(), 30, 30, NULL);
							}
							return 1;
							break;
					default:
							return 0;
							break;
			}
	}
	
	return 0;
}
